﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.WinUI.Hr;
using ESOL.Common;

namespace ESOL.WinUI.Fa
{
    public partial class frmManageAssetUsed : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FaAssetsEntity mSelectedAsset;
        #endregion

        #region Construction
        public frmManageAssetUsed()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FAS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "FAS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtAssetType.Tag != null && txtAssetType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                FaAssetsTypeEntity type = (FaAssetsTypeEntity)txtAssetType.Tag;
                condition += " AND (AT1.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT2.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT3.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT4.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT5.ASSET_TYPE_ID = '" + type.AssetTypeId + "') ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetCode.Text) == false)
            {
                condition += " AND A.ASSET_CODE LIKE '%" + txtAssetCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtAssetSpec.Text) == false)
            {
                condition += " AND A.ASSET_SPEC = '" + txtAssetSpec.Text + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetName.Text) == false)
            {
                condition += " AND A.ASSET_NAME LIKE '%" + txtAssetName.Text + "%' ";
            }
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity entity = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND D.DEPARTMENT_ID = '" + entity.DepartmentId + "' ";
            }
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtPerson.Tag;
                condition += " AND P.PERSON_ID = '" + entity.PersonId + "' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "AT2.ASSET_TYPE_NAME, AT1.ASSET_TYPE_NAME, A.ASSET_CODE";
            DataTable dt = FaAssetsBs.GetUsedInfoByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void ResetButtonEnable()
        {
            btnReturn.Enabled = false;
            btnScrap.Enabled = false;
            btnSale.Enabled = false;
            btnSalesInfo.Enabled = false;
            if (mSelectedAsset == null) 
            {
                return;
            }
            if (mSelectedAsset.Status == "FAS01")
            {
                btnSale.Enabled = true;
                btnScrap.Enabled = true;
            }
            else if(mSelectedAsset.Status=="FAS02")
            {
                btnReturn.Enabled = true;
            }
            else if (mSelectedAsset.Status == "FAS03")
            {
                btnSalesInfo.Enabled = true;
            }

        }
        #endregion

        private void txtAssetType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm = new frmMaintainAssetTypeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAssetType != null)
            {
                txtAssetType.Text = frm.SelectedAssetType.AssetTypeName;
                txtAssetType.Tag = frm.SelectedAssetType;
            }
            else
            {
                txtAssetType.Text = "";
                txtAssetType.Tag = null;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Text = frm.SelectedPerson.NameCn;
                txtPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPerson.Text = "";
                txtPerson.Tag = null;
            }
        }

        private void frmManageAssetUsed_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            ResetButtonEnable();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAsset = new FaAssetsEntity(dr);
            ResetButtonEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAsset frm = new frmMaintainAsset();
            frm.ThisActionType = ActionType.View;
            frm.mSelectedAsset = mSelectedAsset;
            frm.ShowDialog();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            FaUsedEntity used = FaUsedBs.GetByID(mSelectedAsset.AssetId);
            DialogResult dr = MessageBox.Show("是否确定归还选中固定资产？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FaAssetsBs.Return(mSelectedAsset);
                if (successful)
                {
                    MessageBox.Show("固定资产归还成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnScrap_Click(object sender, EventArgs e)
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定报废选中固定资产？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FaAssetsBs.ScrapAsset(mSelectedAsset);
                if (successful)
                {
                    MessageBox.Show("固定资产报废成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSale_Click(object sender, EventArgs e)
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSaleAsset frm = new frmMaintainSaleAsset(mSelectedAsset);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnSalesInfo_Click(object sender, EventArgs e)
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSaleAsset frm = new frmMaintainSaleAsset(mSelectedAsset);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }


        

    }
}
